package com.medvalley.tcm.service.impl;

import com.medvalley.tcm.base.BusinessException;
import com.medvalley.tcm.dao.SickInfoDao;
import com.medvalley.tcm.dao.SickInfoStorageDao;
import com.medvalley.tcm.dao.WenZhenDao;
import com.medvalley.tcm.domain.SickInfo;
import com.medvalley.tcm.domain.SickInfoStorage;
import com.medvalley.tcm.domain.WenZhen;
import com.medvalley.tcm.pojo.SickInfoBrief;
import com.medvalley.tcm.service.specification.SickInfoSpecification;
import com.medvalley.tcm.vo.inputvo.SickInfoSearchInputVo;
import com.medvalley.tcm.vo.inputvo.WenZhenTizhiInputVo;
import com.medvalley.tcm.vo.inputvo.WenZhenZhenXingInputVo;
import com.medvalley.tcm.vo.outputvo.WenZhenOutputVo;
import com.medvalley.tcm.vo.outputvo.WenZhenReportOutputVoBrief;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class WenZhenService {
    @Autowired
    private WenZhenDao wenZhenDao;

    @Autowired
    private SickInfoDao sickInfoDao;

    @Autowired
    private SickInfoStorageDao sickInfoStorageDao;


    public void saveTizhi(WenZhenTizhiInputVo wenZhenTizhiInputVo) {
        WenZhen wenZhen = wenZhenDao.findOneByBarcode(wenZhenTizhiInputVo.getBarcode());
        if (wenZhen == null) {
            throw new BusinessException("没有找到该问诊记录");
        }
        BeanUtils.copyProperties(wenZhenTizhiInputVo, wenZhen);
        wenZhenDao.save(wenZhen);
    }

    public void saveZhengXing(WenZhenZhenXingInputVo wenZhenZhenXingInputVo) {
        WenZhen wenZhen = wenZhenDao.findOneByBarcode(wenZhenZhenXingInputVo.getBarcode());
        if (wenZhen == null) {
            throw new BusinessException("没有找到该问诊记录");
        }
        BeanUtils.copyProperties(wenZhenZhenXingInputVo, wenZhen);
        wenZhenDao.save(wenZhen);
    }

    public WenZhenOutputVo getDetail(long id) {
        WenZhen wenZhen = wenZhenDao.findById(id).orElseThrow(() -> new BusinessException("没有找到该问诊"));
        WenZhenOutputVo wenZhenOutputVo = new WenZhenOutputVo();
        BeanUtils.copyProperties(wenZhen, wenZhenOutputVo);
        return wenZhenOutputVo;
    }

    public List<WenZhenReportOutputVoBrief> search(SickInfoSearchInputVo wenZhenSearchInputVo) {
        List<WenZhenReportOutputVoBrief> wenZhenReportOutputVoBriefs = new ArrayList<>();
        SickInfoSpecification sickInfoSpecification = new SickInfoSpecification(wenZhenSearchInputVo);
        List<SickInfo> sickInfoList = sickInfoDao.findAll(sickInfoSpecification);
        if (CollectionUtils.isEmpty(sickInfoList)) {
            return wenZhenReportOutputVoBriefs;
        }
        List<Long> sickIds = sickInfoList.stream().map(SickInfo::getId).collect(Collectors.toList());
        List<WenZhen> wenZhenList = wenZhenDao.findAllByMedicalIdIn(sickIds);
        Map<Long, WenZhen> wenZhenMap = wenZhenList.stream().collect(Collectors.toMap(WenZhen::getMedicalId, Function.identity()));
        List<String> barcodes = sickInfoList.stream().map(SickInfo::getBarCode).collect(Collectors.toList());
        List<SickInfoStorage> sickInfoStorages = sickInfoStorageDao.findAllByBarCodeIn(barcodes);
        Map<String, SickInfoStorage> stringSickInfoStorageMap = new HashMap<>();
        if (sickInfoStorages != null) {
            stringSickInfoStorageMap = sickInfoStorages.stream().collect(Collectors.toMap(SickInfoStorage::getBarCode, Function.identity()));
        }
        for (SickInfo s : sickInfoList) {
            WenZhenReportOutputVoBrief sickInfoBrief = new WenZhenReportOutputVoBrief();
            BeanUtils.copyProperties(s, sickInfoBrief);
            WenZhen wenZhen = wenZhenMap.get(s.getId());
            if (wenZhen != null) {
                sickInfoBrief.setWenZhenId(wenZhen.getId());
                sickInfoBrief.setPeopleTizhiMainResult(wenZhen.getPeopleTizhiMainResult());
                sickInfoBrief.setZhengxingResult(wenZhen.getZhengxingResult());
                sickInfoBrief.setStandardTizhiMainResult(wenZhen.getStandardTizhiMainResult());
            }
            SickInfoStorage sickInfoStorage = stringSickInfoStorageMap.get(s.getBarCode());
            if (sickInfoStorage != null) {
                sickInfoBrief.setPdfUrl(sickInfoStorage.getFilePath());
            }
            wenZhenReportOutputVoBriefs.add(sickInfoBrief);
        }


        return wenZhenReportOutputVoBriefs;
    }
}
